using System;
using BitMango;
using BitMango.Diagnostics;
using Data;
using TMPro;
using UnityEngine;

public class LevelSceneHandler : MonoBehaviour, IGameScene, IScrollLevel, IHandlerBase, IScrollPage
{
	public void OnChangedPage(tk2dUIScrollPage scrollPage, GameObject page, int pageIndex)
	{
		Debugger.Assert(scrollPage != null, "LevelSceneHandler OnChangedPage");
		Debugger.Assert(page != null, "LevelSceneHandler OnChangedPage");
		this.SetArrow();
	}

	public void OnUpdatePage(tk2dUIScrollPage scrollPage, GameObject page, int pageIndex)
	{
		Debugger.Assert(scrollPage != null, "LevelSceneHandler OnUpdatePage");
		Debugger.Assert(page != null, "LevelSceneHandler OnUpdatePage");
	}

	public void OnUpdateLevel(tk2dUIScrollPage scrollPage, GameObject level, int pageIndex, int levelIndex, out bool enable)
	{
		Debugger.Assert(scrollPage != null, "LevelSceneHandler OnUpdateLevel");
		Debugger.Assert(level != null, "LevelSceneHandler OnUpdateLevel");
		if (levelIndex >= DataContainer<Data.Root>.Data.currentPackage.levels.Length)
		{
			enable = false;
		}
		else
		{
			enable = true;
			LevelWidgetHandler component = level.GetComponent<LevelWidgetHandler>();
			component.SetData(DataContainer<Data.Root>.Data.currentPackage.levels[levelIndex]);
		}
	}

	public void DoDataExchange()
	{
		this.SetUIPosition();
		if (DataContainer<Data.Root>.Data.currentPackage == null)
		{
			DataContainer<Data.Root>.Data.currentPackage = DataContainer<Data.Root>.Data.packages[0];
		}
		if (DataContainer<Data.Root>.Data.currentPackage.levels[0].record.locked)
		{
			DataContainer<Data.Root>.Data.currentPackage.levels[0].record.locked = false;
			DataContainer<Data.Root>.Data.currentPackage.levels[0].SaveContext();
		}
		Package currentPackage = DataContainer<Data.Root>.Data.currentPackage;
		int pageCount = (DataContainer<Data.Root>.Data.currentPackage.levels.Length % 20 != 0) ? (DataContainer<Data.Root>.Data.currentPackage.levels.Length / 20 + 1) : (DataContainer<Data.Root>.Data.currentPackage.levels.Length / 20);
		this.pager.PageCount = pageCount;
		this.pager.ChangePage(currentPackage.record.page, false, 0.5f);
		this.title.text = DataContainer<Data.Root>.Data.currentPackage.display;
		this.SetArrow();
	}

	public void OnEnterScene(object param)
	{
	}

	public void OnLeaveScene()
	{
	}

	private void SetArrow()
	{
		if (this.pager.PageIndex == 0 && this.pager.PageIndex == this.pager.PageCount - 1)
		{
			this.arrowRight.SetActive(false);
			this.arrowLeft.SetActive(false);
		}
		else if (this.pager.PageIndex == 0)
		{
			this.arrowRight.SetActive(true);
			this.arrowLeft.SetActive(false);
		}
		else if (this.pager.PageIndex == this.pager.PageCount - 1)
		{
			this.arrowRight.SetActive(false);
			this.arrowLeft.SetActive(true);
		}
		else
		{
			this.arrowRight.SetActive(true);
			this.arrowLeft.SetActive(true);
		}
	}

	private void OnClickRight()
	{
		BMSoundHelper.Play("se_btnclick", false, 1f);
		this.pager.GoToNextPage();
	}

	private void OnClickLeft()
	{
		BMSoundHelper.Play("se_btnclick", false, 1f);
		this.pager.GoToPrevPage();
	}

	private void OnClick_Form_back()
	{
		BMSoundHelper.Play("se_btnclick", false, 1f);
		Debugger.Assert(DataContainer<Data.Root>.Data.currentPackage != null, "currentPackage : null");
		if (DataContainer<Data.Root>.Data.currentPackage == null)
		{
			Scene.ChangeTo("PackageScene", null);
		}
		Scene.ChangeTo("PackageScene", null);
	}

	private void OnClickQuest()
	{
		BMSoundHelper.Play("se_btnclick", false, 1f);
		BlockPopupTransition.PopupAnimation(delegate
		{
			Scene.ShowPopup("QuestPopup", null, null);
		});
	}

	private void SetUIPosition()
	{
		this.top.transform.localPosition = new Vector3(this.top.transform.localPosition.x, BlockBannerAnchor.GetTopPositionY(), this.top.transform.localPosition.z);
		float bannerPositionY = BlockBannerAnchor.GetBannerPositionY();
		this.bottom.transform.localPosition = new Vector3(this.bottom.transform.localPosition.x, bannerPositionY, this.bottom.transform.localPosition.z);
		Vector3 vector = Scene.CurrentScene().transform.InverseTransformPoint(this.topDown.transform.position);
		Vector3 vector2 = Scene.CurrentScene().transform.InverseTransformPoint(this.bottomUp.transform.position);
		Vector3 localPosition = this.scrollableArea.transform.localPosition;
		this.scrollableArea.transform.localPosition = new Vector3(localPosition.x, (vector.y + vector2.y) / 2f, localPosition.z);
	}

	public void OnClick_stage(LevelWidgetHandler handler)
	{
		Debugger.Assert(handler != null, "LevelSceneHandler OnClick_stage");
		if (!Profile.IsEnableCheat)
		{
			if (handler.data.record.locked)
			{
				return;
			}
		}
		BMSoundHelper.Play("se_btnclick", false, 1f);
		DataContainer<Data.Root>.Data.currentLevel = handler.data;
		Scene.ChangeTo("PlayScene", null);
	}

	private void OnClickCollection()
	{
		BMSoundHelper.Play("se_btnclick", false, 1f);
		DWATTHelper.logEvent("Collection", new object[]
		{
			"Enter"
		});
		Scene.ChangeTo("CollectionThemeScene", "LevelScene");
	}

	private void OnEscape()
	{
		Scene.ChangeTo("PackageScene", null);
	}

	[SerializeField]
	private tk2dCamera cam;

	[SerializeField]
	private GameObject curtain;

	public TextMeshPro title;

	public tk2dUIScrollPage pager;

	public tk2dUIScrollableArea scrollableArea;

	public GameObject top;

	public GameObject bottom;

	public GameObject topDown;

	public GameObject bottomUp;

	public GameObject arrowRight;

	public GameObject arrowLeft;

	public const int width = 4;

	public const int height = 5;

	public const int group = 20;
}
